package org.tjuscs.bulletgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class BulletGame implements ApplicationListener {
	private OrthographicCamera camera;
	private Stage stage;
	private Texture texture;
	private Image image;
	private Action action;

	@Override
	public void create() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();

		stage = new Stage(w, h, true);
		camera = new OrthographicCamera(w, h);
		stage.setCamera(camera);

		texture = new Texture(Gdx.files.internal("j2t.bmp"));
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		addImage();

		InputMultiplexer im = new InputMultiplexer(new InputProc(),
				new GestureDetector(new GestureProc()));

		Gdx.input.setInputProcessor(im);
	}

	private void addImage() {
		image = new Image(texture);
		image.setSize(256, 256);
		stage.addActor(image);

		action = Actions
				.parallel(
						Actions.forever(Actions.rotateBy(360, 3)),
						Actions.forever(Actions.sequence(
								Actions.alpha(0.5f, 0.5f),
								Actions.alpha(1.0f, 0.5f))),
						Actions.forever(Actions.sequence(
								Actions.color(
										new Color(1.0f, 0.1f, 0.1f, 1.0f), 1.0f),
								Actions.color(
										new Color(0.1f, 1.0f, 0.1f, 1.0f), 1.0f),
								Actions.color(
										new Color(0.1f, 0.1f, 1.0f, 1.0f), 1.0f),
								Actions.color(
										new Color(1.0f, 1.0f, 1.0f, 1.0f), 1.0f))),
						Actions.forever(Actions.sequence(
								Actions.scaleTo(0.5f, 1.0f, 2.0f),
								Actions.scaleTo(0.5f, 0.5f, 2.0f),
								Actions.scaleTo(1.0f, 0.5f, 2.0f),
								Actions.scaleTo(1.0f, 1.0f, 2.0f))));
		action.setActor(image);
		image.addAction(action);
	}

	@Override
	public void dispose() {
		texture.dispose();
		stage.dispose();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		camera.update();
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();

		Gdx.app.log("libgdx-demo", "FPS:" + Gdx.graphics.getFramesPerSecond());
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	private class InputProc implements InputProcessor {

		@Override
		public boolean keyDown(int keycode) {
			return false;
		}

		@Override
		public boolean keyUp(int keycode) {
			return false;
		}

		@Override
		public boolean keyTyped(char character) {
			return false;
		}

		@Override
		public boolean touchDown(int screenX, int screenY, int pointer,
				int button) {
			return false;
		}

		@Override
		public boolean touchUp(int screenX, int screenY, int pointer, int button) {
			return false;
		}

		@Override
		public boolean touchDragged(int screenX, int screenY, int pointer) {
			return false;
		}

		@Override
		public boolean mouseMoved(int screenX, int screenY) {
			return false;
		}

		@Override
		public boolean scrolled(int amount) {
			return false;
		}
	}

	private class GestureProc implements GestureListener {

		@Override
		public boolean touchDown(float x, float y, int pointer, int button) {
			return false;
		}

		@Override
		public boolean tap(float x, float y, int count, int button) {
			return false;
		}

		@Override
		public boolean longPress(float x, float y) {
			addImage();
			return true;
		}

		@Override
		public boolean fling(float velocityX, float velocityY, int button) {
			return false;
		}

		@Override
		public boolean pan(float x, float y, float deltaX, float deltaY) {
			camera.translate(-deltaX, deltaY);
			return true;
		}

		@Override
		public boolean zoom(float initialDistance, float distance) {
			return false;
		}

		@Override
		public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
				Vector2 pointer1, Vector2 pointer2) {
			return false;
		}

	}
}
